package com.example.mongodb.mongodb.service;

import com.example.mongodb.mongodb.dao.InitMogodbDao;
import com.example.mongodb.mongodb.entity.Customer;

import com.example.mongodb.mongodb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class InitMogodbService {


    @Autowired
    private InitMogodbDao initMogodbDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    public void setInitMogodbDao(){
        for (int i = 2000; i < 4000; i++) {
            Customer user = new Customer();
            user.setId(i);
            user.setCard(i+"1-E-C-X");
            user.setName("TOMC"+i);
            mongoTemplate.insert(user);
        }

    }


    public void initMongodbUser(){
        for (int i = 2000; i < 4000; i++) {
            User user = new User();
            user.setId(i);
            user.setName(i+"test"+i);
            user.setAge(i+"");
            mongoTemplate.insert(user);
        }

    }


    public void deleteMongodb() {
        mongoTemplate.dropCollection("user");
        mongoTemplate.dropCollection("customer");
    }

    public List<User> queryUserInfo() {
        Query query = new Query(Criteria.where("name").is("2009test2009"));

        List<User> user = mongoTemplate.find(query, User.class, "user");
        return user;
    }
}
